﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using InversPong.Model;
using Microsoft.Xna.Framework.Graphics;
using InversPong.Utilities;
using InversPong.Widgets;
using InversPong.Screens;

namespace InversPong.Views
{
    public class MapView : DrawableGameComponent, GameView
    {
        private SpriteBatch spriteBatch;
        private SpriteFont spriteFontMain;

        private Map Map { get; set; }
        private Vector2 Origin { get; set; }

        private Texture2D Vortex { get; set; }

        private DualScrollMessage dualScrollMessage;

        private ScreenManager ScreenManager { get; set; }

        public MapView(Microsoft.Xna.Framework.Game game, ScreenManager screenManager, Vector2 origin, Map map, GameController gameController)
            : base(game)
        {
            Origin = origin;
            gameController.PowerUpPicked += new EventHandler<EventArgs<PowerUp>>(Controller_PowerUpPicked);
            gameController.LifeLosed += new EventHandler<EventArgs<Player>>(Controller_LifeLosed);
            gameController.GameFinished += new EventHandler<EventArgs<Player>>(Controller_GameFinished);
            Map = map;
            ScreenManager = screenManager;
            game.Components.Add(this);


            dualScrollMessage = new DualScrollMessage(game);
            dualScrollMessage.LeftText = "GAME ";
            dualScrollMessage.RightText = "STARTED";
            dualScrollMessage.Visible = true;
            DrawOrder = Order.StaticLevel;
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            spriteFontMain = Game.Content.Load<SpriteFont>("message");
            Vortex = Game.Content.Load<Texture2D>("gfx\\vortex");
        }

        public void RemoveComponents()
        {
            Game.Components.Remove(dualScrollMessage);
        }

        public override void Draw(GameTime gameTime)
        {

            Services.ISpriteDrawer spriteDrawer = (Services.ISpriteDrawer)Game.Services.GetService(typeof(Services.ISpriteDrawer));

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            
            foreach(Bat bat in Map.Bats) 
            {
                Color color = Color.White;
                if (bat.ContainsPowerUp(PowerUp.Types.Invulnerability))
                {
                    color *= 0.2f;
                }
                spriteDrawer.DrawObject(spriteBatch, bat, color, Origin);
            }

            foreach (MovableObject movable in Map.Movables)
            {
                spriteDrawer.DrawObject(spriteBatch, movable, Color.White, Origin);
            }

            spriteBatch.Draw(Vortex, new Vector2(Origin.X + (Map.Width / 2 - Vortex.Width / 2), Origin.Y + (Map.Height / 2 - Vortex.Height / 2)), Color.White);
            spriteBatch.End(); 

        }

        private void Controller_LifeLosed(object sender, EventArgs<Player> e)
        {
            Player player = e.Value;

            Services.ISoundPlayer soundPlayer = (Services.ISoundPlayer)Game.Services.GetService(typeof(Services.ISoundPlayer));
            soundPlayer.Play("bombe", player.Bat.Position);
        }

        private void Controller_PowerUpPicked(object sender, EventArgs<PowerUp> e)
        {
            PowerUp powerUp = e.Value;
            
            Services.ISoundPlayer soundPlayer = (Services.ISoundPlayer)Game.Services.GetService(typeof(Services.ISoundPlayer));
            soundPlayer.Play(powerUp.GfxName, powerUp.Position);
        }

        private void Controller_GameFinished(object sender, EventArgs<Player> e)
        {
            Player player = e.Value;

            dualScrollMessage.LeftText = player.Name.ToUpper();
            dualScrollMessage.RightText = " LOSED !";
            dualScrollMessage.Visible = true;
            dualScrollMessage.HoldTimer = TimeSpan.FromDays(7);


            ScreenManager.AddScreen(new RestartMenuScreen(), null);
        }
    }
}
